package edu.hubu.talentmanagementsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.CaseFormat;
import edu.hubu.talentmanagementsystem.mapper.StaffChatImgMapper;
import edu.hubu.talentmanagementsystem.mapper.context.MapperContext;
import edu.hubu.talentmanagementsystem.model.ext.StaffChatImg;
import edu.hubu.talentmanagementsystem.service.IStaffChatImgService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import edu.hubu.talentmanagementsystem.common.EncodeOperation;
import edu.hubu.talentmanagementsystem.common.EncodeParam;

import java.util.List;
import java.util.Optional;

/**
 * @author moonlan
 * date 2021/3/2 下午8:45
 */
@SuppressWarnings(value = "unused")
@Service
@Transactional(rollbackFor = Exception.class)
public class StaffChatImgServiceImpl extends ServiceImpl<StaffChatImgMapper, StaffChatImg> implements IStaffChatImgService {

    private final MapperContext context;

    public StaffChatImgServiceImpl(MapperContext context) {this.context = context;}

    @Override
    public boolean deleteById(Integer id) {
        return context.getStaffChatImgMapper().deleteById(id) > 0;
    }

    @Override
    @EncodeOperation
    public Optional<StaffChatImg> insert(@EncodeParam StaffChatImg record) {
        int insert = context.getStaffChatImgMapper().insert(record);
        if (insert > 0) {
            return Optional.of(context.getStaffChatImgMapper().selectById(record.primaryKeyValue()));
        }
        return Optional.empty();
    }

    @Override
    public Optional<StaffChatImg> selectById(Integer id) {
        QueryWrapper<StaffChatImg> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<StaffChatImg> lambdaQueryWrapper = getColumnWrapper(wrapper.lambda()).eq(StaffChatImg::getStaffChatImgId, id);
        return Optional.of(context.getStaffChatImgMapper().selectOne(lambdaQueryWrapper));
    }

    @Override
    public List<StaffChatImg> selectAll(String order) {
        QueryWrapper<StaffChatImg> wrapper = new QueryWrapper<>();
        if (!StringUtils.hasText(order)) {
            LambdaQueryWrapper<StaffChatImg> select = getColumnWrapper(wrapper.lambda());
            return context.getStaffChatImgMapper().selectList(select);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        LambdaQueryWrapper<StaffChatImg> select = getColumnWrapper(wrapper.lambda());
        return context.getStaffChatImgMapper().selectList(select);
    }

    @Override
    public Page<StaffChatImg> selectAllPaged( Integer currentPage,
             Integer pageSize, String order) {
        QueryWrapper<StaffChatImg> wrapper = new QueryWrapper<>();
        Page<StaffChatImg> page = new Page<>(currentPage, pageSize);
        if (!StringUtils.hasText(order)) {
            LambdaQueryWrapper<StaffChatImg> select = getColumnWrapper(wrapper.lambda());
            return context.getStaffChatImgMapper().selectPage(page, select);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        LambdaQueryWrapper<StaffChatImg> select = getColumnWrapper(wrapper.lambda());
        return context.getStaffChatImgMapper().selectPage(page, select);
    }

    private LambdaQueryWrapper<StaffChatImg> getColumnWrapper(LambdaQueryWrapper<StaffChatImg> wrapper) {
        return wrapper.select(
                StaffChatImg::getResourceImgId
                    ,
                StaffChatImg::getStaffChatImgId
                    ,
                StaffChatImg::getChatId
        );
    }

    @Override
    public Page<StaffChatImg> selectLikePaged( Integer currentPage,
             Integer pageSize, StaffChatImg record, String order) {
        Page<StaffChatImg> page = new Page<>(currentPage, pageSize);
        QueryWrapper<StaffChatImg> wrapper = new QueryWrapper<>();
        if (!StringUtils.hasText(order)) {
            return context.getStaffChatImgMapper().selectPage(page, getColumnWrapper(getQueryWrapper(wrapper, record)));
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        return context.getStaffChatImgMapper().selectPage(page, getColumnWrapper(getQueryWrapper(wrapper, record)));
    }

    private LambdaQueryWrapper<StaffChatImg> getQueryWrapper(QueryWrapper<StaffChatImg>wrapper, StaffChatImg record) {
        LambdaQueryWrapper<StaffChatImg> lambda = wrapper.lambda();
        if (record.getResourceImgId() != null) {
            lambda = lambda.eq(StaffChatImg::getResourceImgId,record.getResourceImgId());
        }
        if (record.getStaffChatImgId() != null) {
            lambda = lambda.eq(StaffChatImg::getStaffChatImgId,record.getStaffChatImgId());
        }
        if (record.getChatId() != null) {
            lambda = lambda.eq(StaffChatImg::getChatId,record.getChatId());
        }
        return lambda;
    }

    public int count(StaffChatImg record) {
        return context.getStaffChatImgMapper().selectCount(getQueryWrapper(new QueryWrapper<>(), record));
    }

    @Override
    @EncodeOperation
    public Optional<StaffChatImg> updateById(Integer id, @EncodeParam StaffChatImg record) {
        record.setStaffChatImgId(id);
        int update = context.getStaffChatImgMapper().updateById(record);
        if (update > 0) {
            QueryWrapper<StaffChatImg> wrapper = new QueryWrapper<>();
            LambdaQueryWrapper<StaffChatImg> lambdaQueryWrapper = getColumnWrapper(wrapper.lambda()).eq(StaffChatImg::getStaffChatImgId, id);
            return Optional.of(context.getStaffChatImgMapper().selectOne(getColumnWrapper(lambdaQueryWrapper)));
        }

        return Optional.empty();
    }
}
